//
//  MyView.m
//  两个label的自动布局
//
//  Created by keisun on 2018/3/20.
//  Copyright © 2018年 keisun. All rights reserved.
//

#import "MyView.h"
#import "Masonry.h"

@interface MyView ()

@property(nonatomic,strong)UILabel *leftLab;
@property(nonatomic,strong)UILabel *rightLab;
@end

@implementation MyView

/*
 需求如下：有两个label同一高度，并排宽度是固定的，即效果如下\
      ________________________
     |              |         |
     |Are you Ok?Boy|hello    |
     |______________|_________|
 
 右边label内容过多时要如下效果：
      ________________________
     |              |         |
     |Are you Ok?Boy|helloABCD|
     |______________|come     |
                    |_________|
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.leftLab];
        [self addSubview:self.rightLab];
    }
    return self;
}

-(void)layoutSubviews{
    //这句很关键，UILayoutConstraintAxisHorizontal与UILayoutPriorityDefaultHigh表示水平方向优先级最高，水平方向不要扯我。
     [_leftLab setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    
    /*没有这句左边的label会拉长，而右边的label边界紧贴文字缩成一团,如：
      ________________________
     |                  |     |
     |Are you Ok?Boy    |hello|     X
     |__________________|_____|
     
     */

    
    [_leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(400);
        make.height.mas_equalTo(18);
    }];
    
    [_rightLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_leftLab.mas_right);
        make.top.mas_equalTo(_leftLab);
        make.right.mas_offset(0);
    }];
}

-(UILabel *)leftLab{
    if (!_leftLab) {
        _leftLab = [[UILabel alloc] init];
        _leftLab.backgroundColor = [UIColor greenColor];
        _leftLab.text = @"优先宽度自适应的label";
    }
    return _leftLab;
}

-(UILabel *)rightLab{
    if (!_rightLab) {
        _rightLab = [[UILabel alloc] init];
        _rightLab.backgroundColor = [UIColor redColor];
        _rightLab.text = @"label2";
    }
    return _rightLab;
}

@end
